package twu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by twer on 7/10/14.
 */
public class PrimeFactorFinder {
    public List generate(int num) {
        List<Integer> list = new ArrayList<Integer>();
        if (num == 1) {
            return null;
        }
        else if (num == 2){
            list.add(2);
            return list;
        }
        for (int i = 2;i <= (int) java.lang.Math.sqrt(num) ; i++ ){
            if(num%i == 0){
                list.add(i);
                List<Integer> branchList = generate(num/i);
                return  merge(list,branchList);
            }
        }
       if(list.isEmpty()){
           list.add(num);
       }
       return list;
    }

    private List merge(List<Integer> list, List<Integer> branchList) {
        for(int i = 0; i < branchList.size(); i++){
            int factor = branchList.get(i);
            if(!list.contains(factor)){
                list.add(factor);
            }
        }
        return list;
    }
}
